package cn.xf.controller;

import cn.xf.domain.Orders;
import cn.xf.domain.Product;
import cn.xf.domain.Role;
import cn.xf.domain.UserInfo;
import cn.xf.service.IOrdersService;
import cn.xf.service.IProductService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

/**
 * @author xiaofu
 * @date 2021-05-11 12:34
 * @email dearaf@163.com
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    //查询所有商品
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page", required = true, defaultValue = "1") Integer page, @RequestParam(name = "pageSize", required = true, defaultValue = "10") Integer pageSize) {

        ModelAndView mv = new ModelAndView();
        List<Product> pl = productService.findAllByPage(page,pageSize);
        PageInfo<Product> pageInfo = new PageInfo<>(pl);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("product/product-list");
        return mv;
    }

    //产品添加
    @RequestMapping("/save.do")
    public String save(Product product) {
        productService.save(product);
        return "redirect:findAll.do";
    }

    //产品编辑
    @RequestMapping("/updateById.do")
    public String updateById(Product product) {
        productService.updateById(product);
        return "redirect:findAll.do";
    }

    //产品批量删除
    @RequestMapping("/deleByIds.do")
    public String deleByIds(String ids) {
        // ids格式为：最后有一个逗号
        // 9B1A84DF4A394018A695386120835B0F,676C5BD1D35E429A8C2E114939C5685A,
        ids = ids.substring(0, ids.lastIndexOf(","));
        List<String> idList = Arrays.asList(ids);
        System.out.println("即将删除如下产品：" + idList);
        //删除时需要级联删除，需要完善...
        productService.deleByIds(idList);
        return "redirect:findAll.do";
    }

    //产品批量更改状态

    /**
     * 在传参时，如果只有一个参数，可以不配置param，如果有两个参数，需要配置，否则会报错
     *
     * @param ids
     * @param status
     * @return
     */
    @RequestMapping("/changeStatusByIds.do")
    public String changeStatusByIds(@RequestParam("ids") String ids,@RequestParam("status") String status) {
        List<String> idList = Arrays.asList(ids);
        System.out.println("即将更改如下产品的状态：" + idList);
        //删除时需要级联删除，需要完善...
        productService.changeStatusByIds(idList, status);
        return "redirect:findAll.do";
    }

    /**
     * @param id
     * @return
     */
    //根据ID查询产品并返回编辑页面
    //handleId为0，表示详情，若为1，代表编辑
    @RequestMapping("/selectById.do")
    public ModelAndView selectById(@RequestParam("id") String id, @RequestParam("handleId") String handleId) {

        ModelAndView mv = new ModelAndView();
        Product pro = productService.selectById(id);
        mv.addObject("pro", pro);
        if ("0".equals(handleId)) {
            mv.setViewName("product/product-detail");
        } else {
            mv.setViewName("product/product-audit");
        }
        return mv;
    }

    @Autowired
    private IOrdersService ordersService;

    /**
     * 根据ID查找此产品下所有的订单，返回产品展示订单界面
     *
     * @param id
     * @return
     */
    @RequestMapping("/findOrdersByProductId.do")
    public ModelAndView findOrdersByProductId(String id) {
        Product product = productService.selectById(id);
        //查找此用户可以添加的角色的名称
        List<Orders> ordersList = ordersService.findByProductId(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("product", product);
        mv.addObject("ordersList", ordersList);
        mv.setViewName("/product/product-orders");
        return mv;
    }
}
